package com.hmall.item.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.constant.RedisConstant;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.common.dto.Restore;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 分页查询商品
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> page(Integer page, Integer size) {
        Page<Item> itemPage = new Page<>(page, size);
        Page<Item> itemPage1 = itemMapper.selectPage(itemPage, null);

        PageDTO pageDTO = BeanUtil.toBean(itemPage1, PageDTO.class);
        pageDTO.setList(itemPage1.getRecords());
        return pageDTO;
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void isEnable(Long id, Integer status) {
        this.lambdaUpdate()
                .set(Item::getStatus, status)
                .eq(Item::getId, id)
                .update();

        rabbitTemplate.convertAndSend("preheat.exchange", "preheat.all", "删除redis");
        this.preheatItems();
    }

    /**
     * 预热
     */
    @Override
    public void preheatItems() {
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Item::getSold);

        Page<Item> itemPage = new Page<>(1, 20);
        itemPage = itemMapper.selectPage(itemPage, wrapper);

        redisTemplate.opsForValue().set(RedisConstant.PREHEAT_LIST, JSONUtil.toJsonStr(itemPage.getRecords()));
        redisTemplate.opsForValue().set(RedisConstant.PREHEAT_TOTAL, String.valueOf(itemPage.getTotal()));
    }

    /**
     * 恢复库存
     * @param list
     */
    public void restore(List<Restore> list){

        for (Restore restore : list) {
            // 查询该id之前的库存
            Item item = itemMapper.selectById(restore.getItemId());
            if(ObjUtil.isEmpty(item)){
                log.info("商品不存在");
            }
            item.setStock(item.getStock() + restore.getNum());
            item.setUpdateTime(LocalDateTime.now());

            LambdaUpdateWrapper<Item> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Item::getId, item.getId());

            itemMapper.update(item, wrapper);
        }

    }
}
